package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.SignBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SpannableStringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 任务中心
 */
public class TaskCenterActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_question)
    TextView tvQuestion;
    @BindView(R.id.tv_sign_days)
    TextView tvSignDays;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_bg_one)
    TextView tvBgOne;
    @BindView(R.id.iv_day_one)
    ImageView ivDayOne;
    @BindView(R.id.tv_day_one_money)
    TextView tvDayOneMoney;
    @BindView(R.id.tv_bg_two)
    TextView tvBgTwo;
    @BindView(R.id.iv_day_two)
    ImageView ivDayTwo;
    @BindView(R.id.tv_day_two_money)
    TextView tvDayTwoMoney;
    @BindView(R.id.tv_bg_three)
    TextView tvBgThree;
    @BindView(R.id.iv_day_three)
    ImageView ivDayThree;
    @BindView(R.id.tv_day_three_money)
    TextView tvDayThreeMoney;
    @BindView(R.id.tv_bg_four)
    TextView tvBgFour;
    @BindView(R.id.iv_day_four)
    ImageView ivDayFour;
    @BindView(R.id.tv_day_four_money)
    TextView tvDayFourMoney;
    @BindView(R.id.tv_bg_five)
    TextView tvBgFive;
    @BindView(R.id.iv_day_five)
    ImageView ivDayFive;
    @BindView(R.id.tv_day_five_money)
    TextView tvDayFiveMoney;
    @BindView(R.id.tv_bg_six)
    TextView tvBgSix;
    @BindView(R.id.iv_day_six)
    ImageView ivDaySix;
    @BindView(R.id.tv_day_six_money)
    TextView tvDaySixMoney;
    @BindView(R.id.tv_bg_seven)
    TextView tvBgSeven;
    @BindView(R.id.iv_day_seven)
    ImageView ivDaySeven;
    @BindView(R.id.tv_day_seven_money)
    TextView tvDaySevenMoney;
    @BindView(R.id.tv_real_name_state)
    TextView tvRealNameState;
    @BindView(R.id.tv_real_name_money)
    TextView tvRealNameMoney;
    @BindView(R.id.tv_real_person_state)
    TextView tvRealPersonState;
    @BindView(R.id.tv_real_person_money)
    TextView tvRealPersonMoney;
    @BindView(R.id.tv_invite_state)
    TextView tvInviteState;
    @BindView(R.id.iv_invite_image)
    ImageView ivInviteImage;
    @BindView(R.id.tv_invite_title)
    TextView tvInviteTitle;
    @BindView(R.id.tv_invite_money)
    TextView tvInviteMoney;
    private SignBean data;

    public static void actionStar(Context context) {
        Intent intent = new Intent(context, TaskCenterActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_task_center;
    }

    @Override
    protected void initView() {
        getSignInfo();
    }

    private void getSignInfo() {
        OkGoUtil.postRequest(getContext(), UrlConstants.SIGN_INFO, null, new JsonCallback<BaseModel<SignBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<SignBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        data = response.body().getData();
                        setView();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<SignBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void setView() {
        setSignView();

        if(data.getIs_real_name() == 0){
            tvRealNameState.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvRealNameState.setText("去完成");
        }else{
            tvRealNameState.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvRealNameState.setText("已完成");
        }

        if(data.getIs_real_avatar() == 0){
            tvRealPersonState.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvRealPersonState.setText("去完成");
        }else{
            tvRealPersonState.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvRealPersonState.setText("已完成");
        }

        tvRealNameMoney.setText(data.getReal_name());
        tvRealPersonMoney.setText(data.getReal_avatar());
    }

    private void setSignView() {

        tvSignDays.setText("已经签到 "+data.getCount() +" 天");

        if(data.getCount() > 0){
            tvBgOne.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDayOneMoney.setVisibility(View.GONE);
            ivDayOne.setVisibility(View.VISIBLE);
        }else{
            tvBgOne.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDayOneMoney.setVisibility(View.VISIBLE);
            tvDayOneMoney.setText(data.getArr().getOne());
            ivDayOne.setVisibility(View.GONE);
        }

        if(data.getCount() > 1){
            tvBgTwo.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDayTwoMoney.setVisibility(View.GONE);
            ivDayTwo.setVisibility(View.VISIBLE);
        }else{
            tvBgTwo.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDayTwoMoney.setVisibility(View.VISIBLE);
            tvDayTwoMoney.setText(data.getArr().getTwo());
            ivDayTwo.setVisibility(View.GONE);
        }

        if(data.getCount() > 2){
            tvBgThree.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDayThreeMoney.setVisibility(View.GONE);
            ivDayThree.setVisibility(View.VISIBLE);
        }else{
            tvBgThree.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDayThreeMoney.setVisibility(View.VISIBLE);
            tvDayThreeMoney.setText(data.getArr().getThree());
            ivDayThree.setVisibility(View.GONE);
        }

        if(data.getCount() > 3){
            tvBgFour.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDayFourMoney.setVisibility(View.GONE);
            ivDayFour.setVisibility(View.VISIBLE);
        }else{
            tvBgFour.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDayFourMoney.setVisibility(View.VISIBLE);
            tvDayFourMoney.setText(data.getArr().getFour());
            ivDayFour.setVisibility(View.GONE);
        }

        if(data.getCount() > 4){
            tvBgFive.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDayFiveMoney.setVisibility(View.GONE);
            ivDayFive.setVisibility(View.VISIBLE);
        }else{
            tvBgFive.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDayFiveMoney.setVisibility(View.VISIBLE);
            tvDayFiveMoney.setText(data.getArr().getFive());
            ivDayFive.setVisibility(View.GONE);
        }

        if(data.getCount() > 5){
            tvBgSix.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDaySixMoney.setVisibility(View.GONE);
            ivDaySix.setVisibility(View.VISIBLE);
        }else{
            tvBgSix.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDaySixMoney.setVisibility(View.VISIBLE);
            tvDaySixMoney.setText(data.getArr().getSix());
            ivDaySix.setVisibility(View.GONE);
        }

        if(data.getCount() > 6){
            tvBgSeven.setBackgroundResource(R.drawable.shape_gray_bg_30);
            tvDaySevenMoney.setVisibility(View.GONE);
            ivDaySeven.setVisibility(View.VISIBLE);
        }else{
            tvBgSeven.setBackgroundResource(R.drawable.shape_pink_corners_30);
            tvDaySevenMoney.setVisibility(View.VISIBLE);
            tvDaySevenMoney.setText(data.getArr().getSeven());
            ivDaySeven.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_back, R.id.tv_question,R.id.tv_real_name_state, R.id.tv_real_person_state, R.id.tv_invite_state,R.id.tv_sign})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_question:
                // TODO: 2021/11/4  常见问题
                break;
            case R.id.tv_real_name_state:
                if(data.getIs_real_name() == 0){
                    RealNameAuthActivity.actionStar(context);
                }else{
                    ToastUtil.showToast(context,"实名认证已完成或正在审核中");
                }
                break;
            case R.id.tv_real_person_state:
                if(data.getIs_real_avatar() == 0){
                    RealPersonAuthActivity.actionStar(context);
                }else{
                    ToastUtil.showToast(context,"真实头像认证已完成或正在审核中");
                }
                break;
            case R.id.tv_invite_state:
                InviteFriendActivity.actionStar(context);
                break;
            case R.id.tv_sign:
                sign();
                break;
        }
    }

    private void sign() {
        OkGoUtil.postRequest(context, UrlConstants.SIGN, null, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        getSignInfo();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e("TAG",response.getException().getMessage());
            }
        });
    }
}